
#include <game_state/running/switching/game_running_switching.hpp>
#include <level/level.hpp>

#include <input/inputSingleton.h>
#include <input/actions/actions.hpp>
#include <window/window.hpp>
#include <game_state/running/playing/game_running_playing.hpp>

#include <level/messages/level_over.hpp>

float const fade_out_time = 1.1f;

game_running_switching_state::game_running_switching_state( level_ptr level_ )
	:		m_level( level_ )
		,	m_valid( true )
		,	m_origional_active( level_->active( ) )
		,	m_quitting(false)
		,	m_fade_out_timer(0.0f)
{
	g_user_input.removeAction( actions::enter_switch_mode );
	g_user_input.assignActionToKey( actions::select_slide, sf::Key::Space );
	g_user_input.assignActionToKey( actions::enter_switch_mode, sf::Key::LControl );
	g_user_input.assignActionToKey( actions::slide_cycle_left, sf::Key::Left );
	g_user_input.assignActionToKey( actions::slide_cycle_right, sf::Key::Right );

	m_messenger_connection = g_messenger.add_listener( level_over_message::name( ), boost::bind( &game_running_switching_state::message_callback, this, _1, _2 ) );
}

game_running_switching_state::~game_running_switching_state( )
{
}

game_state_ptr game_running_switching_state::step( float delta_time )
{
	if ( !m_quitting )
	{
		//OutputDebugStringA( "=========Testing Player=========\n" );

		::player & player = m_level->player( );
		level::slide_ptr slide = m_level->current_slide( );

		m_valid = player.can_add_to_world( slide->world( ) );

		if ( m_valid && g_user_input.getActionInput( actions::select_slide ).status == keyPressed )
		{
			player.add_to_world( slide->world( ) );
			return game_state_ptr( new game_running_playing_state( m_level ) );
		}
		else if ( g_user_input.getActionInput( actions::enter_switch_mode ).status == keyPressed )
		{
			m_level->active( m_origional_active );
			return game_state_ptr( new game_running_playing_state( m_level ) );
		}

		if ( g_user_input.getActionInput( actions::slide_cycle_left ).status == keyPressed )
		{
			m_level->previous_slide( );
		}
		else if ( g_user_input.getActionInput( actions::slide_cycle_right ).status == keyPressed )
		{
			m_level->next_slide( );
		}
	}
	else
	{
		if ( m_fade_out_timer >= fade_out_time )
		{
			return game_state_ptr( );
		}

		m_fade_out_timer += delta_time;
	}

	return shared_from_this( );
}

void game_running_switching_state::draw( )
{
	m_level->draw( );

	if ( !m_valid )
	{
		sf::Shape rectangle = sf::Shape::Rectangle( sf::Vector2f( 0.0f, 0.0f ), sf::Vector2f( g_window->GetWidth( ), g_window->GetHeight( ) ),
													sf::Color( 255, 0, 0, 128 ) );

		g_window->Draw( rectangle );
	}

	if ( m_quitting )
	{
		sf::Shape rectangle = sf::Shape::Rectangle( sf::Vector2f( 0.0f, 0.0f ), sf::Vector2f( g_window->GetWidth( ), g_window->GetHeight( ) ),
													sf::Color( 0, 0, 0, static_cast<sf::Uint8>( std::min( 1.0f, m_fade_out_timer ) * 255 ) ) );

		g_window->Draw( rectangle );
	}
}

void game_running_switching_state::message_callback( std::string const & message_name, message_data_ptr data )
{
	if ( message_name == level_over_message::name( ) )
	{
		m_quitting = true;
		m_fade_out_timer = 0.0f;
	}
}
